package database;

import java.sql.CallableStatement;
import java.sql.Types;

import clientMain.Login;
import clientMain.Main;

public class Paging {
	
	/**
	 * @author Tidus
	 * @param tblName String
	 * @category dung cho cac bang, view binh thuong
	 * @return totalPage
	 */
	public static int getTotalPage(String tblName){
		int totalPage;
		try {
			Login.conn.setAutoCommit(false);
			CallableStatement cst = Login.conn.prepareCall("{call sp_TotalPageTbl(?,?,?)}");
			cst.setString(1, tblName);
			cst.setInt(2, Main.record);
			cst.registerOutParameter(3, Types.INTEGER);
			cst.executeUpdate();
			Login.conn.commit();
			totalPage = cst.getInt(3);
		} catch (Exception e) {
			totalPage = -1;
			e.printStackTrace();
		}
		return totalPage;
	}
	
	/**
	 * @author Tidus
	 * @param DepId int
	 * @category lay totalPage NV cua tung phong ban
	 * @return totalPage
	 */
	public static int getTotalPage(int depId){
		int totalPage;
		try {
			Login.conn.setAutoCommit(false);
			CallableStatement cst = Login.conn.prepareCall("{call sp_TotalPageEmInDep(?,?,?)}");
			cst.setInt(1, depId);
			cst.setInt(2, Main.record);
			cst.registerOutParameter(3, Types.INTEGER);
			cst.executeUpdate();
			Login.conn.commit();
			totalPage = cst.getInt(3);
		} catch (Exception e) {
			totalPage = -1;
			e.printStackTrace();
		}
		return totalPage;
	}
	
	/**
	 * @author Tidus
	 * @param monthYear[mm/yyyy]
	 * @param depId int
	 * @category lay totalPage NV cua tung phong ban
	 * @return totalPage
	 */
	public static int getTotalPage(String monthYear, int depId){
		int totalPage;
		try {
			Login.conn.setAutoCommit(false);
			CallableStatement cst = Login.conn.prepareCall("{call sp_TotalPageSSInDep(?,?,?,?)}");
			cst.setString(1, monthYear);
			cst.setInt(2, depId);
			cst.setInt(3, Main.record);
			cst.registerOutParameter(4, Types.INTEGER);
			cst.executeUpdate();
			Login.conn.commit();
			totalPage = cst.getInt(4);
		} catch (Exception e) {
			totalPage = -1;
			e.printStackTrace();
		}
		return totalPage;
	}

	/**
	 * @author Tidus
	 * @param String column, String search
	 * @category tra ve so kq(NV) tim duoc
	 * @return totalRec
	 */
	public static int getTotalRecord(String column, String search) {
		int totalRec;
		try {
			Login.conn.setAutoCommit(false);
			CallableStatement cst = Login.conn.prepareCall("{call sp_TotalPageSearch(?,?,?,?)}");
			cst.setInt(1, Main.comId);
			cst.setString(2, column);
			cst.setString(3, search);
			cst.registerOutParameter(4, Types.INTEGER);
			cst.executeUpdate();
			Login.conn.commit();
			totalRec = cst.getInt(4);
		} catch (Exception e) {
			totalRec = -1;
			e.printStackTrace();
		}
		return totalRec;
	}
}
